﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SimpleUnitity
{
    public static class TypeConvert
    {
#if CSharp3

        public static DbData AsDbData(this object value)
        {
            return ToDbData(value);
        }

#endif

        public static DbData ToDbData(object value)
        {
            if (value == null) { return null; }
            DbData data = new DbData();
            var type = value.GetType();
            var properties = type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);
            foreach (var prop in properties)
            {
                if (prop.CanRead)
                {
                    data[prop.Name] = prop.GetValue(value, null);
                }
            }
            return data;
        }

        public static DbData ToDbData(IDictionary<string, object> datas)
        {
            if (datas == null) { return null; }
            return new DbData(datas);
        }

        public static DbData ToDbData<T>(IDictionary<string, T> datas)
        {
            if (datas == null) { return null; }
            DbData data = new DbData();
            foreach (var item in datas)
            {
                data[item.Key] = item.Value;
            }
            return data;
        }


    }
}
